<?php
/**
 * Elgg page handler
 *
 * If page_handler() fails, send to front page.
 *
 * @package Elgg
 * @subpackage Core
 */

// Load Elgg engine
// define('externalpage',true);
require_once("../start.php");

function getIP() { 
	$ip; 
	if (getenv("HTTP_CLIENT_IP")) 
		$ip = getenv("HTTP_CLIENT_IP"); 
	else if(getenv("HTTP_X_FORWARDED_FOR")) 
		$ip = getenv("HTTP_X_FORWARDED_FOR"); 
	else if(getenv("REMOTE_ADDR")) 
		$ip = getenv("REMOTE_ADDR"); 
	else 
		$ip = "UNKNOWN";
	return $ip; 
}
function getMicroTime() {
    $timer = explode(' ', microtime());
    $timer = $timer[1] + $timer[0];
    return $timer;
}
function curPageURL() {
    $pageURL = 'http';
	if (isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == "on") {
		$pageURL .= "s";
	}
    $pageURL .= "://";
    if ($_SERVER["SERVER_PORT"] != "80") {
        $pageURL .= $_SERVER["SERVER_NAME"] . ":" . $_SERVER["SERVER_PORT"] . $_SERVER["REQUEST_URI"];
    } else {
        $pageURL .= $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
    }
    return $pageURL;
}

$logData = TRUE;
if ($logData) {
    $startTime = getMicroTime();
}

// Get input
$handler = get_input('handler');
$page = get_input('page');


// Call the page handler functions
if (!page_handler($handler, $page)) {
	forward();
}

if ($logData) {
    $doneTime = round(getMicroTime() - $startTime, 4);
    $myFile = "logs-" . date("j-n-Y") . ".txt";
    $fh = fopen($myFile, 'a');
    $stringData = date('H:i:s') . " " . curPageURL() . " " . $doneTime . " " . getIP()  . " \n";
    fwrite($fh, $stringData);
    if (isset($_GET['debug_api'])) {
	echo $stringData;		
    }
    fclose($fh);
}


